﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace ERPStore.Models
{
	[Serializable]
	[DataContract]
	public class Price
	{
		private Price()
		{
		}

		public Price(decimal input, double taxRate)
			: this()
		{
			Value = input;
			TaxRate = taxRate;
		}

		public Price(double input, double taxRate)
			: this(Convert.ToDecimal(input), taxRate)
		{
		}


		public Price(float input, double taxRate)
			: this(Convert.ToDecimal(input), taxRate)
		{
		}

		#region Properties

		[DataMember]
		public decimal Value { get; set; }
		[DataMember]
		public double TaxRate { get; set; }
		public decimal TaxValue
		{
			get
			{
				return Value * Convert.ToDecimal(TaxRate);
			}
		}
		public decimal ValueWithTax
		{
			get
			{
				
				return Value * Convert.ToDecimal(1 + TaxRate);
			}
		}

		#endregion

		private System.Globalization.CultureInfo m_CultureInfo;
		public System.Globalization.CultureInfo CultureInfo
		{
			get
			{
				if (m_CultureInfo != null)
				{
					return m_CultureInfo;
				}
				m_CultureInfo = new System.Globalization.CultureInfo("en-us");
				m_CultureInfo.NumberFormat.CurrencyDecimalSeparator = ".";
				m_CultureInfo.NumberFormat.NumberDecimalSeparator = ".";
				return m_CultureInfo;
			}
		}

		[Obsolete("Use Method Exension instead", false)]
		public string IntegerPart
		{
			get
			{
				var text = Value.ToString(CurrencyPattern, CultureInfo);
				var parts = text.Split('.');
				return parts[0].Replace(","," ");
			}
		}

		[Obsolete("Use Method Exension instead", false)]
		public string IntegerPartWitTax
		{
			get
			{
				var text = ValueWithTax.ToString(CurrencyPattern, CultureInfo);
				var parts = text.Split('.');
				return parts[0].Replace(",", " ");
			}
		}

		[Obsolete("Use Method Exension instead", false)]
		public string DecimalPart
		{
			get
			{
				var text = Value.ToString(CurrencyPattern, CultureInfo);
				var parts = text.Split('.');
				return parts[1];
			}
		}

		[Obsolete("Use Method Exension instead", false)]
		public string DecimalPartWithTax
		{
			get
			{
				var text = ValueWithTax.ToString(CurrencyPattern, CultureInfo);
				var parts = text.Split('.');
				return parts[1];
			}
		}

		[Obsolete("Use Method Exension instead", false)]
		public string CurrencyPattern 
		{
			get
			{
				return "#,##0.00";
			}
		}

	}
}
